using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class XEffectTypeWriter : MonoBehaviour
{
    public Text effectText;


    private void Awake()
    {
        effectText = GetComponent<Text>();
        if (!effectText)
            throw new System.NullReferenceException();
    }

    public void StartEffect(float stepTime, string word, System.Action complete)
    {
        StartCoroutine(OnStartWriter(stepTime, word, complete));
    }

    IEnumerator OnStartWriter(float stepTime, string word, System.Action complete)
    {
        int nWriterPosition = 1;
        while(nWriterPosition < word.Length)
        {
            effectText.text = word.Substring(0, nWriterPosition);
            yield return new WaitForSecondsRealtime(stepTime);
            nWriterPosition ++;
        }

        effectText.text = word;

        if (complete != null) {
            complete.Invoke();
        }
    }
}
